package com.kelaite.procurement.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.dto.PurchaseOrderExecutionItemsDTO;
import com.kelaite.procurement.entity.TPurchaseOrderExecution;
import com.kelaite.procurement.repository.PurchaseOrderExecutionItemsRepository;
import com.kelaite.procurement.repository.PurchaseOrderExecutionRepository;
import com.kelaite.procurement.repository.PurchaseOrderItemsRepository;
import com.kelaite.procurement.repository.SalesOrderExecutionItemsRepository;
import com.kelaite.procurement.repository.SalesOrderExecutionRepository;
import com.kelaite.procurement.repository.SalesOrderItemsRepository;
import com.kelaite.procurement.service.PurchaseOrderExecutionService;
import com.kelaite.procurement.utils.CurrentUserUtils;

@Service
@Transactional
public class PurchaseOrderExecutionServiceImpl implements PurchaseOrderExecutionService{
  
	@Autowired
	private PurchaseOrderExecutionRepository purchaseOrderExecutionRepository; 
	
	@Autowired
	private SalesOrderExecutionRepository salesOrderExecutionRepository;
	
	@Autowired
	private SalesOrderExecutionItemsRepository salesOrderExecutionItemsRepository;
	
	@Autowired
	private PurchaseOrderExecutionItemsRepository purchaseOrderExecutionItemsRepository;
	
	@Autowired
	private SalesOrderItemsRepository salesOrderItemsRepository;
	
	@Autowired
	private PurchaseOrderItemsRepository purchaseOrderItemsRepository;
	
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public TPurchaseOrderExecution save(TPurchaseOrderExecution persistence) {
		return this.purchaseOrderExecutionRepository.save(persistence);
	}

	@Override
	public TPurchaseOrderExecution getObject(Long id) {
		return this.purchaseOrderExecutionRepository.findOne(id);
	}

	@Override
	public List<TPurchaseOrderExecution> getAll() {
		return this.purchaseOrderExecutionRepository.findAll();
	}

	@Override
	public void deleteByObject(TPurchaseOrderExecution persistence) {
		this.purchaseOrderExecutionRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		this.purchaseOrderExecutionRepository.delete(id);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TPurchaseOrderExecution> getByProperty(String property, Object value) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TPurchaseOrderExecution.class).add(Restrictions.eq(property, value));
		return criteria.list();
	}

	@Transactional
	@Override
	public int confirmPurchaseOrderExecution(String[] orderExecutionIds,List<PurchaseOrderExecutionItemsDTO> purchaseOrderExecutionItems) {

		int i=0;
		String cUser = CurrentUserUtils.getInstance().getUsername();

		for(String tempStr : orderExecutionIds)
		{
			if(!"".equals(tempStr))
			{	
				Long tempId=Long.parseLong(tempStr);
				//����PurchaseOrderExecution��
				this.purchaseOrderExecutionRepository.updatePurchaseOrderExecution(tempId, new Date(), new Date(), cUser);
				//����SalesOrderExecution��
				this.salesOrderExecutionRepository.updateSalesOrderExecution(tempId, new Date(), new Date(), cUser);
			}
			++i;
		}
		
		for(PurchaseOrderExecutionItemsDTO purchaseOrderExecutionItem :purchaseOrderExecutionItems)
		{
			Long id=purchaseOrderExecutionItem.getId();
			Double quantityConfirmed=purchaseOrderExecutionItem.getQuantityDelivered();		
			//����T_Purchase_Order_Execution_Items��
			this.purchaseOrderExecutionItemsRepository.updatePurchaseOrderExecutionItems(id, quantityConfirmed, new Date(), cUser);
			//����T_Sales_Order_Execution_Items��
			this.salesOrderExecutionItemsRepository.updateSalesOrderExecutionItems(id, quantityConfirmed, new Date(), cUser);
			
			//����T_Purchase_Order_Items��
			this.purchaseOrderItemsRepository.updatePurchaseOrderItems(id, quantityConfirmed,new Date(), new Date(), cUser);
			//����T_Sales_Order_Items��
			this.salesOrderItemsRepository.updateSalesOrderItems(id, quantityConfirmed,new Date(), new Date(), cUser);
		}
		
		return i;
	}
	
}
